/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.NewActionHelp;
import com.ciko.code.bean.UserBean;
import com.shwread.android.qysw.activity.ForgetPwdActivity;
import com.shwread.android.qysw.activity.NewLoginActivity;
import com.shwread.android.qysw10000243.R;
import com.shwread.http.base.TextHttpResponseHandler;
import com.shwread.httpsdk.constant.ResultCode;
import com.shwread.httpsdk.http.callback.ActionListener;
import com.shwread.httpsdk.util.Base64;
import com.tgx.tina.android.ipc.framework.IUIPage;

import org.apache.http.Header;

import base.tina.core.task.infc.ITaskResult;
import logic.dao.UserDao;
import logic.util.Util;


/**
 * @Project： Lqyd_Android
 * @Title：CheckCodeGetPage
 * @Description：忘记密码第三步设置新密码
 * @Author：wayne
 * @Date：2014年8月14日下午4:20:54
 */
public class CheckCodeSetNewPwdPage
        extends
        AbstractUIPage<ForgetPwdActivity>
        implements
        OnClickListener {

    private TextView tvTitle;
    private Button btnBack;
    private Button btnFinish;
    private EditText newPwd;
    private String accountString;
    private String verifyCodeString;
    private EditText confirmPwd;
    private String mPassword1;

    private LinearLayout llayShowPwd;
    private ImageView ivShowPwd;
    private TextView tvShowPwd;
    private boolean Is_SHOW_PWD = false;

    public void setAccountString(String accountString) {
        this.accountString = accountString;
    }

    public void setVerifyCodeString(String verifyCodeString) {
        this.verifyCodeString = verifyCodeString;
    }

    /**
     * <p>
     * Title:
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @param t
     */
    public CheckCodeSetNewPwdPage(ForgetPwdActivity t) {
        super(t);
    }

    @Override
    public <E extends Activity> View createView(E arg0, int arg1) {
        if (null != curMyView) return curMyView;
        curMyView = bActivity.getLayoutInflater().inflate(R.layout.login_forget_pwd_finish, null);

        initView();
        setListener();

        return curMyView;
    }

    void initView() {
        tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
        tvTitle.setText("设置新密码");
        btnBack = (Button) curMyView.findViewById(R.id.common_header_back);
        btnBack.setVisibility(View.VISIBLE);
        btnFinish = (Button) curMyView.findViewById(R.id.login_forget_pwd_finish_btn);
        newPwd = (EditText) curMyView.findViewById(R.id.login_forget_pwd_new_pwd_et);
//		confirmPwd = (EditText) curMyView.findViewById(R.id.login_forget_pwd_confirm_pwd_et);
        llayShowPwd = (LinearLayout) curMyView.findViewById(R.id.login_forget_pwd_show_pwd_llay);
        ivShowPwd = (ImageView) curMyView.findViewById(R.id.login_forget_pwd_show_pwd_iv);
        tvShowPwd = (TextView) curMyView.findViewById(R.id.login_forget_pwd_show_pwd_tv);

    }

    void setListener() {
        btnBack.setOnClickListener(this);
        btnFinish.setOnClickListener(this);
        ivShowPwd.setOnClickListener(this);
//		llayShowPwd.setOnClickListener(this);
//		tvShowPwd.setOnClickListener(this);
    }

    @Override
    public int enter(IUIPage<?> arg0) {
        ivShowPwd.setSelected(false);
        newPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        return 0;
    }

    @Override
    public int leave(IUIPage<?> arg0) {
        return 0;
    }

    @Override
    public void notifyView(int arg0, Bundle arg1) {

    }

    @Override
    public void setStatus(int arg0) {

    }

    @Override
    public View updateView(int arg0, ITaskResult arg1) {
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_header_back:
                Intent intent = new Intent(bActivity, NewLoginActivity.class);
                bActivity.startActivity(intent);
                bActivity.finish();
                break;
            case R.id.login_forget_pwd_finish_btn:
//				if (newPwd.getText().toString().length() < 6) Util.showToast(bActivity, "阁下，密码这么简单真的好吗？");
//				else
//				{
                if (validate()) {
//					new ResetPasswordThirdStepAction(bActivity, accountString, verifyCodeString, mPassword1, new MyAction()).start();
                    NewActionHelp.resetPasswordThirdStep(bActivity, accountString, verifyCodeString, mPassword1, new MyHandler());
                }
                break;
            case R.id.login_forget_pwd_show_pwd_iv:
                clickShowPwd();
                break;
            default:
                break;
        }
    }

    private void clickShowPwd() {
        if (Is_SHOW_PWD) {
            //切换成密文显示
            Is_SHOW_PWD = false;
            ivShowPwd.setSelected(false);
            newPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            //切换成明文显示
            Is_SHOW_PWD = true;
            ivShowPwd.setSelected(true);
            newPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    private boolean validate() {
        mPassword1 = newPwd.getText().toString().trim();
        if (Util.isEmpty(mPassword1)) {
            Util.showToast(bActivity, "请输入新密码");
        } else if (Util.NUllToString(mPassword1).length() < 6 || Util.NUllToString(mPassword1).length() > 16) {
            Util.showToast(bActivity, "密码是6-16位哦");
        } else {
            return true;
        }
        return false;
    }

    private class MyHandler extends TextHttpResponseHandler {

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            if (statusCode > 1000) {
                Util.showToast(bActivity, ResultCode.getResultText(statusCode));
            }

        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            UserDao userDao = UserDao.getInstance(bActivity);
            UserBean bean = userDao.getUser(accountString);
            if (bean != null) {
                bean.setPassword(Base64.encode(mPassword1, "utf-8"));
                userDao.updateUserPwd(bean);
            }

            Util.showToast(bActivity, "新密码设置成功");
            Intent intent = new Intent(bActivity, NewLoginActivity.class);
            bActivity.startActivity(intent);
            bActivity.finish();
        }

    }


    private class MyAction
            implements
            ActionListener {

        @Override
        public void OK(Object obj) {
            bActivity.getUIHandler().post(new Runnable() {
                @Override
                public void run() {
                    UserDao userDao = UserDao.getInstance(bActivity);
                    UserBean bean = userDao.getUser(accountString);
                    if (bean != null) {
                        bean.setPassword(Base64.encode(mPassword1, "utf-8"));
                        userDao.updateUserPwd(bean);
                    }

                    Util.showToast(bActivity, "新密码设置成功");
                    Intent intent = new Intent(bActivity, NewLoginActivity.class);
                    bActivity.startActivity(intent);
                    bActivity.finish();
                }
            });
        }

        @Override
        public void ERROR(int resultCode, final String message) {
            bActivity.getUIHandler().post(new Runnable() {
                @Override
                public void run() {
                    Util.showToast(bActivity, message);
                }
            });
        }

        @Override
        public void HTTPERROR(int statusCode) {
        }
    }
}
